﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace GiMessCommons
{
    //maxim 256 => fielduri pe byte
    public enum Field
    {
        MessageUserOrigin = 1,   
        MessageUserFullName,
        MessageUserDest,       
        MessageText,        

        ListCount,
        ListUserIndex,
        ListUserName,
        ListUserFirstName,
        ListUserLastName,
        ListUserGroup,
        ListUserOnline,
        ListUserStatus,
        ListUserAvatar,

        AuthUserName,
        AuthUserPassword,
        AuthUserFristName,
        AuthUserLastName,

        UpdateUserName,
        UpdateOnline,
    }

    //public enum FieldList
    //{
    //    ListUserNames,
    //    ListFirstNames,
    //    ListLastNames,
    //}

    public class DataFieldsWriter
    {
        private StringBuilder sb;
        //private const string delimiter = "À€";//new string(new char[] { (char)192, (char)128 });

        public DataFieldsWriter()
        {
            sb = new StringBuilder();
        }

        public void WriteField(Field field, string data)
        {
            sb.Append((char)field);
            sb.Append(NetConsants.FieldsDelimiter);
            sb.Append(data);
            sb.Append(NetConsants.FieldsDelimiter);
        }

        public string GetString()
        {
            return sb.ToString();
        }
    }

    public class DataFieldsReader
    {
        private class DataField
        {
            public Field Field     { get; set; }
            public string FieldData { get; set; }

            public DataField(Field field, string fieldData)
            {
                Field     = field;
                FieldData = fieldData;
            }

            public DataField(string field, string fieldData)
            {
                Field     = (Field)field.ToCharArray()[0];
                FieldData = fieldData;
            }
        }

        //private StringBuilder sb;
        private ArrayList dataFieldsList;

        public DataFieldsReader(string data)
        {
            string[] delim          = { NetConsants.FieldsDelimiter };
            string[] dataFields     = data.Split(delim, StringSplitOptions.None);
                     dataFieldsList = new ArrayList(dataFields.Length);
    
            for (int i = 0; i < dataFields.Length - 1; i += 2)
            {
                dataFieldsList.Add(new DataField(dataFields[i], dataFields[i+1]));
            }
        }

        public string GetFieldData(Field field)
        {
            foreach(DataField df in dataFieldsList)
            {
                if (df.Field == field)
                    return df.FieldData;
            }
            return null;
        }

        public string GetFieldData(Field field, int count)
        {
            int pos = -1;
            foreach (DataField df in dataFieldsList)
            {
                if (df.Field == field)
                {
                    pos++;
                    if (pos == count)
                        return df.FieldData;
                }
            }
            return null;
        }
    }
}